package P05多线程锁.P05_Callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class P01_Demo1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        new Thread(new MyRun(),"AA").start();
        // 报错
//        new Thread(new MyCall(),"AA").start();
        FutureTask<Integer> futureTask1 = new FutureTask<>(new MyCall());
        //等价于以下代码
        FutureTask<Integer> futureTask2 = new FutureTask<>(() -> {
            System.out.println(Thread.currentThread().getName() + " come in callable.");
            return 1024;
        });
        new Thread(futureTask2,"futureTask2").start();
        new Thread(futureTask1,"futureTask1").start();
//        while (!futureTask2.isDone()) {
//            System.out.println("waiting....");
//        }
        System.out.println(futureTask2.get());
        System.out.println(futureTask1.get());

        System.out.println(Thread.currentThread().getName() + " come over");
    }
}

class MyRun implements Runnable {

    @Override
    public void run() {

    }
}

class MyCall implements Callable {

    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName() + " come in callable.");
        return 200;
    }
}
